package com.derbysoft.ota.application.translator.rs;

import java.util.Collection;
import java.util.Map.Entry;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.interfaces.protocol._2009a.OTACancelRS;
import com.derbysoft.ota.interfaces.protocol._2009a.UniqueIDType;
import com.derbysoft.remote.dswitch.hotel.dto.CancelRS;
import com.derbysoft.remote.dswitch.hotel.dto.HotelReservationIDDTO;
import com.google.common.base.Function;
import com.googlecode.functionalcollections.FunctionalIterables;

public class CancelRSTranslator implements
		Translator<Entry<CancelRS, OTACancelRS>, OTACancelRS> {

	public OTACancelRS translate(Entry<CancelRS, OTACancelRS> source) {
		CancelRS switchRS = source.getKey();
		OTACancelRS otaCancelRS = source.getValue();
		otaCancelRS.getUniqueID().addAll(ids(switchRS));
		return otaCancelRS;
	}

	private Collection<UniqueIDType> ids(CancelRS switchRS) {
		return FunctionalIterables.make(switchRS.getHotelReservationIDs()).map(
			new Function<HotelReservationIDDTO, UniqueIDType>() {
				public UniqueIDType apply(HotelReservationIDDTO from) {
					return uniqueID(from);
				}
			}
		).toCollection();
	}
	
	private UniqueIDType uniqueID(HotelReservationIDDTO from) {
		UniqueIDType uniqueIDType = new UniqueIDType();
		uniqueIDType.setID(from.getResIDValue());
		uniqueIDType.setType(from.getResIDType().getCode());
		return uniqueIDType;
	}

}
